<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Config;
use think\Validate;
use GuzzleHttp\Client;
use \app\common\model\Essay as EssayModel;
use \app\api\controller\Ai;

/**
 * 作文表接口
 */
class Essay extends Api
{

    /**
     * 作文列表
     * @var array
     */
    protected $noNeedRight = '*';

    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new EssayModel();
        $this->ai = new Ai();
    }
    /**
     * 作文表
     * @ApiTitle    (查询作文表)
     *  @ApiMethod  (POST)
     *  @ApiParams   (name="user_id", type="int", required=true, description="用户ID")
     *  @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     *  @ApiReturnParams   (name="msg", type="string", required=true, sample="查询成功")
     *  @ApiReturn   ({
          'code':'200',
          'msg':'查询成功',
          'time': "",
          'data': $row
        })
     */

    public function essayIndex()
    {
        $data = input('post.');
        $userId = $data['user_id'];
        $page = isset($data['page']) ? intval($data['page']) : 1;
        $limit = isset($data['limit']) ? intval($data['limit']) : 10;
        if(!$userId || !Validate::is($userId, 'number')){
            $this->error('用户ID错误');
        }

        $dis = [
            'user_id' => $userId,
        ];
        $row = $this->model->dataList($dis);
        
        if (!$row) {
            $this->success('未查询到作文', []); 
        }
        $this->success('查询成功', $row);
        
    }

    /**
     * 作文详情
     * @ApiTitle    (查询作文详情)
     *  @ApiMethod  (GET)
     *  @ApiParams   (name="id", type="int", required=true, description="作文ID")
     *  @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     *  @ApiReturnParams   (name="msg", type="string", required=true, sample="查询成功")
     *  @ApiReturn   ({
         'code':'200',
         'msg':'查询成功',
         'time': "",
         'data': $row
       })
     */
    public function essayDetail()
    {
        $data = input('get.');
        $id = $data['id'];
        if(!$id || !Validate::is($id, 'number')){
            $this->error('作文ID错误');
        }

        $dis = [
            'id' => $id,
        ];
        $row = $this->model->dataInfo($dis);
        if (!$row) {
            $this->success('作文不存在',[]);
        }

        $this->success('查询成功', $row);
    }

    /**
     * ai作文对话
     * @ApiMethod (POST)
     * @ApiParams (name="user_id", type="int", required=true, description="学生id")
     * @ApiParams (name="content", type="text", required=true, description="对话内容")
     */
    public function aiessayTalk()
    {
        $data = input('post.');
        $userId = $data['user_id'];
        $content = $data['content'];
        if(!$userId || !$content || !Validate::is($userId, 'number')){
            $this->error('参数错误');
        }
        $row = $this->ai->callDeepSeekAPIToEssay($content,$userId);
        $this->success('操作成功',$row);
    }

}